package com.furtherstar.auth.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.furtherstar.auth.dao.mapper.AuthUserMapper;
import com.furtherstar.auth.dao.mapper.UserSecretHistoryMapper;
import com.furtherstar.auth.dao.mapper.UserSecretMapper;
import com.furtherstar.auth.dao.model.AuthUser;
import com.furtherstar.auth.dao.model.UserSecret;
import com.furtherstar.auth.dao.model.UserSecretExample;
import com.furtherstar.auth.dao.model.UserSecretHistory;
import com.furtherstar.auth.service.UserSecretService;
import com.furtherstar.auth.util.PasswordUtil;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.lang.IDGenerator;
import com.yummy.security.User;
import com.yummy.web.context.RequestContext;

@Service
public class UserSecretServiceImpl implements UserSecretService{
	
	@Autowired
	private UserSecretMapper userSecretMapper;
	@Autowired
	private UserSecretHistoryMapper userSecretHistoryMapper;
	@Autowired
	private AuthUserMapper authUserMapper;

	@Override
	public UserSecret getByUserid(Long userid) {
		UserSecretExample example = new UserSecretExample();
		example.createCriteria().andUseridEqualTo(userid);
		List<UserSecret> list = userSecretMapper.selectByExample(example);
		return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public void update(UserSecret userSecret) {
		optMark(userSecret);
		userSecretMapper.updateByPrimaryKey(userSecret);
	}

	private void optMark(UserSecret userSecret) {
		User user = RequestContext.getCurrentUser();
		userSecret.setLmNickname(user.getNickname());
		userSecret.setLmTime(new Date());
		userSecret.setLmUserid((Long) user.getUserid());
	}

	@Override
	public void recordHistory(UserSecret userSecret) {
		UserSecretHistory ush = createUserSecretHistory(userSecret);
		userSecretHistoryMapper.insert(ush);
	}

	private UserSecretHistory createUserSecretHistory(UserSecret userSecret) {
		UserSecretHistory ush = new UserSecretHistory();
		ush.setPassword(userSecret.getPassword());
		ush.setUserid(userSecret.getUserid());
		ush.setUsername(userSecret.getUsername());
		ush.setId(IDGenerator.getId());
		User user = RequestContext.getCurrentUser();
		ush.setLmNickname(user.getNickname());
		ush.setLmUserid((Long) user.getUserid());
		ush.setLmTime(new Date());
		return ush;
	}

	@Override
	public void createUserSecret(String password, AuthUser user) {
		UserSecret us = new UserSecret();
		us.setUserid(user.getId());
		us.setUsername(user.getUsername());
		us.setPassword(PasswordUtil.encryptPassword(password, user.getId().toString()));
		createMark(us);
		userSecretMapper.insert(us);
	}

	private void createMark(UserSecret us) {
		us.setId(IDGenerator.getId());
		User user = RequestContext.getCurrentUser();
		us.setLmUserid((Long) user.getUserid());
		us.setLmNickname(user.getNickname());
		Date now = new Date();
		us.setLmTime(now);
	}

	@Override
	public void changePassword(Long userid, String password) {
		AuthUser user = getAuthUser(userid);
		AssertUtil.notNull(user);
		deleteAndRecordOldSecret(user.getId());
		String newPassword = PasswordUtil.encryptPassword(password, user.getId().toString());
		UserSecret userSecret = new UserSecret();
		userSecret.setUserid(userid);
		userSecret.setPassword(newPassword);
		userSecret.setUsername(user.getUsername());
		createMark(userSecret);
		userSecretMapper.insert(userSecret);
	}

	private void deleteAndRecordOldSecret(Long userid) {
		UserSecret old = getByUserid(userid);
		if(old != null) {
			userSecretMapper.deleteByPrimaryKey(old.getId());
			UserSecretHistory ush = createUserSecretHistory(old);
			userSecretHistoryMapper.insert(ush);
		}
	}

	private AuthUser getAuthUser(Long id) {
		return authUserMapper.selectByPrimaryKey(id);
	}

}
